# @Time:2021/6/12 16:26
# @Author:5120195554王欣霏
# -*- coding = utf-8 -*-

from actor.bear_battle import BearBattleStatus,BattleBear
from actor.swk_battle import SwkBattleStatus, SwkBattle
from dialog import blit_text
from pygame.constants import K_y, K_n, K_ESCAPE
import enum
import os
import pygame
import random
from scene import SceneResult


class BattleStatus(enum.IntEnum):
    Swk_dialog = 0  # 孙悟空的对话框状态 当场景建立时就初始化这个状态
    Swk_fight = 1
    Enemy_fight = 2
    Fail = 3
    Win = 4
    Escape = 5


class BattleDialog1(object):
    status = BattleStatus.Swk_dialog

    # def __init__(self, swk_hp: int, monster_group: pygame.sprite.Group):
    def __init__(self, swk_hp: int):
        self.swk = SwkBattle(swk_hp)
        self.monster = BattleBear()
        dialog_file = os.path.join('../resource', 'img', 'dialog', 'dialog.png')
        self.dialog = pygame.image.load(dialog_file)
        self.dialog.set_alpha(255)  # 一开始这里设置的100过于透明
        self.dialog_width = self.dialog.get_width()
        self.dialog_height = self.dialog.get_height()

        font_path = os.path.join('../resource', 'font', 'newfont.TTF')
        self.font = pygame.font.Font(font_path, 18)

        self.text = "呔！妖精！ " \
                    "吃 我 一 棒。y " \
                    "下 次 来 战。n "

        self.over_show = False

        # 设置音效
        self.swk_background_music = pygame.mixer.Sound('../resource/sound/swk.wav')
        self.monster_background_music = pygame.mixer.Sound('../resource/sound/swk.wav')
        pygame.mixer.music.set_volume(0.9)  # 设置音量

    def process(self, key_down: bool, pressed_key: int):
        # 这个判断要放在最前面 否则没有显示结果就会退出
        # 输
        if self.swk.status == SwkBattleStatus.DieOver:
            self.status = BattleStatus.Fail
            self.final = SceneResult.Fail
            if key_down and pressed_key == K_ESCAPE:
                self.over_show = True
            return

        # 赢
        if self.monster.status == BearBattleStatus.DieOver:
            self.status = BattleStatus.Win
            self.final=SceneResult.Win
            if key_down and pressed_key == K_ESCAPE:
                self.over_show = True
            # if len(self.monster_group) == 0:
            #     self.final = SceneResult.Win
            return

        # 逃跑
        if self.swk.status == SwkBattleStatus.EscapeOver:
            self.status = BattleStatus.Escape
            self.final = SceneResult.Fail
            if key_down and pressed_key == K_ESCAPE:
                self.over_show = True
            return

        # 这个判断一定要放在前面 否则是个死循环
        # 判断是否到达最后一帧
        if self.swk.status == SwkBattleStatus.Escape or self.swk.status == SwkBattleStatus.Die:
            self.swk.action_over()
            return

        if self.monster.status == BearBattleStatus.Die:
            self.monster.action_over()
            return

        # 对话框状态
        if self.status == BattleStatus.Swk_dialog:
            # 孙悟空要打的话
            if key_down and pressed_key == K_y:
                self.status = BattleStatus.Swk_fight
                self.swk.set_status(SwkBattleStatus.Fight)
                self.monster.set_status(BearBattleStatus.Station)
            # 孙悟空逃跑
            if key_down and pressed_key == K_ESCAPE or pressed_key == K_n:
                self.status = BattleStatus.Escape
                self.swk.set_status(SwkBattleStatus.Escape)
                self.monster.set_status(BearBattleStatus.Station)

        # 打斗状态

        # 孙悟空打
        if self.status == BattleStatus.Swk_fight:
            if self.swk.action_over():
                self.status = BattleStatus.Enemy_fight
                self.swk.set_status(SwkBattleStatus.Station)
                self.monster.set_status(BearBattleStatus.Fight)
                # self.monster.attack_hp(-50)
                hp = random.randint(1, 30)
                self.monster.attack_hp(-hp)
                self.swk.attack_hp(5)
                self.monster_background_music.play()  # 不循环播放
                return

        # 怪物打
        if self.status == BattleStatus.Enemy_fight:
            if self.monster.action_over():
                self.status = BattleStatus.Swk_dialog  # 询问孙悟空是否要继续打
                self.swk.set_status(SwkBattleStatus.Station)
                self.monster.set_status(BearBattleStatus.Station)
                # self.swk.attack_hp(-10)
                hp = random.randint(1, 15)
                self.swk.attack_hp(-hp)
                # self.swk_background_music.play()
                return

    def draw(self, surface):
        dialog = self.dialog.copy()
        if self.status == BattleStatus.Swk_dialog:  # 当状态为对话框状态时 才会绘制
            blit_text(dialog, self.text, (100, 25), self.font)
        self.swk.draw(dialog)
        self.monster.draw(dialog)
        surface.blit(dialog, (800 / 2 - self.dialog_width / 2, 600 / 2 - self.dialog_height / 2))
